package com.kun.base.web.websocket;

import com.kun.base.model.User;
import com.kun.base.web.activemq.ProducerService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.WebSocketMessage;
import org.springframework.web.socket.WebSocketSession;

import javax.annotation.Resource;
import javax.jms.Destination;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Map;

/**
 * Created by likun on 2017/4/14.
 */
public class SystemWebSocketHandler implements WebSocketHandler {
    private static Logger log = Logger.getLogger(WebSocketHandler.class);

    /**
     * 消息列队服务,可以向消息列队发送消息
     */
    @Autowired
    ProducerService producerService;
    //队列名amq.ssm
    @Resource(name="defaultQueueDestination")
    private Destination queueDestination;

    /**
     * 消息处理
     */
    private WSMessageHandler wsMessageHandler=new WSMessageHandler();

    /**
     * 本台服务器连接的websocket集合
     */
    private static final ArrayList<WebSocketSession> users = new ArrayList<>();

    /**
     * 获取和本服务器连接的websocket session合集
     * @return
     */
    public static ArrayList<WebSocketSession> getUsers(){
        return users;
    }

    /**
     * 用户上线/连接成功(websocket客户端连接到服务成功)
     * @param session
     * @throws Exception
     */
    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        users.add(session);
        Map<String, Object> attributes = session.getAttributes();
        User user= (User) attributes.get("user");
        HttpSession httpSession = (HttpSession) attributes.get("httpSession");
        wsMessageHandler.connectionOpen(session,users,httpSession,user,producerService,queueDestination);
    }

    /**
     * 接收消息(服务端接收到websocket客户端的消息)
     * @param session
     * @param message
     * @throws Exception
     */
    @Override
    public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {
        Map<String, Object> attributes = session.getAttributes();
        User user= (User) attributes.get("user");
        HttpSession httpSession = (HttpSession) attributes.get("httpSession");
        wsMessageHandler.handleMessage(session,users,httpSession,user,producerService,queueDestination,message);
    }

    /**
     * websocket客户端与服务端连接异常或断开
     * @param session
     * @param exception
     * @throws Exception
     */
    @Override
    public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
        if(session.isOpen()){
            session.close();
        }
        users.remove(session);
        log.error("websocket连接异常:", exception);
        Map<String, Object> attributes = session.getAttributes();
        User user= (User) attributes.get("user");
        HttpSession httpSession = (HttpSession) attributes.get("httpSession");
        wsMessageHandler.handleTransportError(session,users,httpSession,user,producerService,queueDestination,exception);
    }

    /**
     * websocket客户端与服务端连接断开
     * @param session
     * @param closeStatus
     * @throws Exception
     */
    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception {
        users.remove(session);
        Map<String, Object> attributes = session.getAttributes();
        User user= (User) attributes.get("user");
        HttpSession httpSession = (HttpSession) attributes.get("httpSession");
        wsMessageHandler.connectionClosed(session,users,httpSession,user,producerService,queueDestination,closeStatus);
    }

    @Override
    public boolean supportsPartialMessages() {
        return false;
    }

}
